<template>
	<view class="">
		<view class="Row3">
			<view class="select">
				<view class="click" @click.native="clickshow" role="button">
					<text>任意分区</text>
					<image src="@/static/xzhu/down.svg" class="icon" :class="{'active':noshowselect}" aria-label="分区选择展开按钮"></image>
				</view>
			</view>
			<view class="list-lr">
					<EmptyWarn v-if="attractions.length === 0">没有符合条件的景点哦~</EmptyWarn>
					<view class="list" v-for="(spot,index) in attractions" :key="spot.id" >
						<view class="pic" @click="openContentPopup(spot)" role="button" aria-label="查看景点详情">
							<image :src="spot.img" mode="aspectFill"></image>
						</view>
						<view class="listbox">
							<text class="third-heading--main">{{spot.name}}</text>
							<text class="third-heading--sub spot-address"> {{ spot.address }}</text>
						</view>
						<view class="add">
							<image src="@/static/plus-square-Bold.svg" class="icon"  @click.stop="addAttraction(spot)"  v-if="!selectedIds.includes(spot.id)" role="button" aria-label="添加景点到路线"></image>
							<image :src="getIconImage(spot.id)" class="icon"  @click.stop="addAttraction(spot)" :aria-label="已添加景点"  v-if="selectedIds.includes(spot.id)"></image>
						</view>
					</view>
			</view>
		</view>
		<!-- 点击任意分区后显示的弹窗 -->
		<view class="popup1" v-if="showselect">
			<view class="keytext">
				<view class="list"  @click="handleChangeDistrict('')" role="button" :class="{'active': curDistrict === ''}">
					<text class="a-text">全部</text>
				</view>
			</view>
			<view class="keytext">
				<view class="list" v-for="(district,index) in districtList" :key="index" @click="handleChangeDistrict(district.name)" role="button" :class="{'active': curDistrict === district.name}">
					<text class="a-text">{{ district.name }}</text>
				</view>
			</view>
			
		</view>
		<!-- 点击加号后显示的小弹窗 -->
		<view v-if="showPopup" class="popup2">
			<view class="content">
				<view class="top"></view>
				<view class="bottom">
					<view class="number">
						<view class="item-box" v-for="(item,index) in selectedIds" v-show="item.id!=1 && item.id!=2">
							<view class="one" @click="clickItem(index)" role="button" :class="{'active': index === clickedIndex}">
								<text>{{index+1}}</text>
							</view>
							<image src="@/static/arrow.svg"></image>
						</view>
					</view>
				</view>
				<view class="addContent" :class="{'active':addContentVisible}">
					<view class="">
						<view class="list" v-if="clickAttraction !== null">
							<view class="pic">
								<image :src="clickAttraction.img" mode="aspectFill"></image>
							</view>
							<view class="listbox" >
								<text class="one">{{clickAttraction.name}}</text>
								<text class="two" v-text="clickAttraction.name"></text>
							</view>
							<view class="add">
								<image :src="getIconImage(clickAttraction.id)" @click="addAttraction(clickAttraction)" role="button"></image>
							</view>
						</view>
					</view>
					<view class="m2">
						<map :longitude="clickAttraction.pointX" :latitude="clickAttraction.pointY" :markers="spotMarker"></map>
					</view>
				</view>
			</view>
		</view>
		<AttractionDetail ref="spotDetail"></AttractionDetail>
	</view>
</template>

<script>
	import EmptyWarn from "@/components/EmptyWarn/EmptyWarn.vue"
	import AttractionDetail from "./AttractionDetail.vue"
	import * as AttractionApi from "@/api/attractions/attraction.js"
	import store from "@/store/index.js"
	import { getConstantDistance } from "@/utils/geo"
	import { isEmpty } from "@/utils/util"

	const districtList = [
					{
						name:'玄武区'
					},{
						name:'秦淮区'
					},{
						name:'鼓楼区'
					},{
						name:'栖霞区'
					},{
						name:'江宁区'
					},{
						name:'浦口区'
					},{
						name:'六合区'
					},{
						name:'高淳区'
					},{
						name:'雨花台'
					},{
						name:'建邺区'
					},{
						name:'溧水区'
					}
				]
	export default {
		emits:["addAttraction",'changeDistrict'],
		props:['selectedIds','selectedItems','attractions','curDistrict'],
		components:{
			AttractionDetail,
			EmptyWarn
		},
		data() {
			return {
				selectQues: false,//是否关闭弹窗，flase为关
				selectedIndex:0,//默认选中的索引(小的标签区域)
				noshowselect:true,//点击之前不是橙色的
				showselect:false,//默认不点击
				goods:0,//默认点击为0
				showPopup: false,//是否开弹窗
				animationData: null, 	
				clickedIndex: -1,
				clickAttraction:null,
				districtList,// 市区列表
				addContentVisible:false
				
			}
		},
		computed:{
			spotMarker(){
				let marker = []
				if (this.clickAttraction === null ){
					marker.push({
						longitude:this.$store.userPosition.pointX,
						latitude:this.$store.userPosition.pointY,
						width:30,
						height:30
					})
				}
				marker.push({
					longitude:clickAttraction.pointX,
					latitude:clickAttraction.pointY,
					iconPath:clickAttraction.img ?? "../../static/images/outdoors.png",
					width:30,
					height:30
				})
				return marker
			}	
		},
		watch:{
			selectedIds(ids){
			    this.showPopup = ids.length > 0
				this.calcDistance()
			},
			deep:true
		},
		
		computed:{
			userPosition(){
				const {pointX,pointY} = store.getters.routeOptions
				if(pointX === undefined || pointY === undefined){
					return null
				}
				return {pointX,pointY}
			  }
		},
		
		methods: {
			async openContentPopup(spot){
				const spotContent = await this.getAttractionInfo(spot.id)
				this.$refs.spotDetail.open(spotContent)
			},
			// 点击任意分区
			clickshow(){
				// 点击之后变成橙色
				this.noshowselect = !this.noshowselect
				this.showselect = !this.showselect
			},
			// 点击下方弹窗的小图标
			clickItem(index){
				if(this.clickedIndex !== index){
					this.clickedIndex = index
					this.clickAttraction = this.selectedItems[index]
					this.addContentVisible = true
				}else{
					this.clickedIndex = -1
					this.clickAttraction = null
					this.addContentVisible = false
				}
			},
			handleChangeDistrict(district){
				this.$emit('changeDistrict',district)
			},
			
			/***   数据请求方法   ***/
			
			async getAttractionInfo(attractionsId){
				return await AttractionApi.getAttractionInfo(attractionsId)	
			},
			addAttraction(spot){
				this.$emit("addAttraction",spot)
				this.calcDistance()
			},
			getIconImage(id){
				const attraction_index = this.selectedIds.indexOf(id)+1
				return `../../static/numbers/${attraction_index}.svg`;
			},
			calcDistance(){
				// 计算路线总长度
				if(isEmpty(this.userPosition)){
					store.dispatch("route/setRouteDistance",0)
					return 
				}
				const distance = getConstantDistance([this.userPosition,...this.selectedItems])
				store.dispatch("route/setRouteDistance",distance)
			},
		
		},
	}
</script>

<style lang="scss" scoped>
	.Row3{
			width: 600rpx;
			display: flex;
			flex-direction: column;
			gap:12px;
			justify-content: flex-start;
			align-items: flex-start;
			.select{
				width: 280px;
				height: 40px;
				display: flex;
				justify-content: flex-start;
				align-items: flex-start;
				.click{
					@include border-four-roundings;
					width: 123px;
					height: 32px;
					display: flex;
					justify-content: center;
					align-items: center;
					text{
						width: 68px;
						height: 24px;
						font-size: 17px;
						font-weight: 700;
						letter-spacing: 0px;
						line-height: 23.17px;
						color: rgba(0, 0, 0, 1);
					}
					.icon{
						transform:rotate(0deg);
						transition:all 0.3s;
						&.active{
							transform:rotate(90deg);
						}
					}
				}
			}
			.list-lr{
				width:100%;
				padding:0px 8px;
				box-sizing: border-box;
				
				overflow-y: scroll;
				overflow-x: visible;
				.list{
					width: 100%;
					height: 73px;
					display: flex;
					flex-direction: row;
					justify-content: flex-start;
					align-items: flex-start;
					margin-bottom: 12px;
					.pic{
						@include border-four-roundings;
						width: 68px;
						height: 68px;
						overflow:hidden;
						
						image{
							width:100%;
							height:100%;
						}
					}
					.listbox{
						margin-left: 6px;
						width: 60%;
						height: 70px;
						display: flex;
						flex-direction: column;
						justify-content: flex-start;
						align-items: flex-start;
						color: rgba(0, 0, 0, 1);
						.spot-address{
							white-space: nowrap;
							text-overflow: ellipsis;
							overflow: hidden;
							width:100%;
						}
					}
					.add{
						margin-top: 27px;
						margin-left: 12px;
						
						
					}
			}
		}
	}
	
	// 点击任意分区后显示的弹窗
	.popup1 {
		@include border-four-roundings;
		position: absolute;
		padding:10px 0px;
		margin: auto;
		left: 38.5px;
		top: 185px;
		width: 80%;
		background: rgba(255, 255, 255, 1);
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		.keytext{
			width: 100%;
			display: flex;
			flex-direction: row;
			justify-content: flex-start;
			align-items: flex-start;
			flex-wrap: wrap;
			.active{
				background-color: rgba(52, 58, 64, 1);
				text{
					color: rgba(255, 255, 255, 1);
				}
			}
			.list{
				@include border-four-roundings;
				margin-left: 6px;
				margin-top: 8px;
				width: auto;
				height: 25px;
				justify-content: center;
				align-items: center;
				.a-text{
					margin-top: 2px;
					display: inline-block;
					padding-left: 7px;
					padding-right: 7px;
				}
			}
		}
	}
	
	// 点击加厚显示的弹窗效果
	.popup2 {
		@include border-four-roundings;
		white-space: pre-wrap;
		flex-wrap: wrap;
		position: fixed;
		margin: auto;
		bottom: 33px;
		width: 600rpx;
		height: auto;
		background: rgba(255, 255, 255, 1);
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		// 弹窗内容
		.content {
			padding-top: 12px;
			padding-bottom: 14px;
			.top{
				margin-left: 109px;
				width: 65px;
				height: 9px;
				border-radius: 10px;
				background: rgba(66, 65, 65, 1);
			}
			.bottom{
				.number{
					margin-left: 6px;
					margin-top: 16px;
					width: 270px;
					height: auto;
					display: flex;
					white-space: pre-wrap;
					flex-wrap: wrap;
					.active{
						background-color: rgba(52, 58, 64, 1);
						text{
							color: rgba(255, 255, 255, 1);
						}
					}
					.one{
						@include border-four-roundings;
						margin-left: 10px;
						margin-right: 7px;
						width: 28px;
						height: 28px;
						display: flex;
						justify-content: center;
						align-items: center;
						font-size: 16px;
						font-weight: 700;
						letter-spacing: 0px;
						line-height: 23.17px;
						color: rgba(33, 37, 41, 1);
					}
					.item-box{
						display: flex;
						flex-direction: row;
						width: 65px;
						image{
							width: 13.33px;
							height: 13.33px;
							margin-top: 9px;
						}
					}
					
				}

			}
			.addContent{
				padding-left: 10rpx;
				display:none;
				&.active{
					display:block;
				}
				.list{
						margin: 10px 0 10px 0;
						width: 270px;
						height: 73px;
						display: flex;
						.pic{
							@include border-four-roundings;
							width: 68px;
							height: 68px;
							overflow:hidden;
							image{
								width:100%;
								height:100%;
							}
						}
						.listbox{
							margin-left: 6px;
							margin-top: 6px;
							width: 158px;
							height: 73px;
							display: flex;
							flex-direction: column;
							.one,.two{
								width: 100%;
								display: -webkit-box; /** 对象作为伸缩盒子模型显示 **/
								overflow: hidden;
								word-break: break-all;  /* break-all(允许在单词内换行。) https://www.w3school.com.cn/cssref/pr_word-break.asp*/
								text-overflow: ellipsis;  /* 超出部分省略号 */
								-webkit-box-orient: vertical; /** 设置或检索伸缩盒对象的子元素的排列方式 **/
								-webkit-line-clamp: 1; /** 显示的行数 **/
							}
							.one{
								height: 24px;
								font-size: 16px;
								font-weight: 700;
								line-height: 23.17px;
								margin-bottom: 19px;
							}
							.two{
								height: 18px;
								font-size: 12px;
								font-weight: 400;
								line-height: 17.38px;
							}
						}
						.add{
							margin-top: 27px;
							margin-left: 12px;
							width: 32px;
							height: 32px;
							image{
								width: 20px;
								height: 20px;
							}
						}
				}
				.m2{
					@include border-four-roundings;
					width:100%;
					box-sizing: border-box;
					height: 240upx;
					overflow:hidden;
					map{
						width: 500upx;
						height: 240upx;
					}
				}
			}
		}
	}
	
</style>